<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class News_model extends CI_Model {
	var $perpage = 10;
	var $url;
	public function __construct() {
	   parent::__construct();
	   $this->url = str_replace(".html", "", $this->uri->segment(1));
	}

	function index($args) {
		$data['url'] = $this->url;
		$id = $args[0];		
		$data['item'] = $item = $this->input->get('item', true);
		$data['from'] = $from = $this->input->get('from', true);
		if ($from == "") $from = 0;
		$this->db->select()->from('news')->where('news_status', 1)->order_by(($id==2?"news_id":"news_date"), 'desc')->limit($this->perpage, $from);
			if ($id != "") $this->db->where('news_parent', $id);
			if (is_numeric($item)) $this->db->where('news_item', $item);
		$data['pages'] = $pages = $this->db->get()->result_array();
		/* pagination */
		$this->db->select()->from('news')->where('news_status', 1);
			if ($id != "") $this->db->where('news_parent', $id);
			if (is_numeric($item)) $this->db->where('news_item', $item);
		$count = $this->db->count_all_results();

		if ($from != 0 && sizeof($pages) <= 0 && $count > 0) {
			$pgng = $from - $this->perpage;
			redirect($this->url.'/?from='.$pgng);
		}
		$this->load->library('pagination');
		$config['base_url'] = '/'.$this->url;
		$config['total_rows'] = $count;
		$config['page_query_string'] = TRUE;
		$config['per_page'] = $this->perpage;
		$this->pagination->initialize($config);
		$data['paging'] = $this->pagination->create_links();
		/* pagination */
		$out['text'] = $this->load->view('modules/news/index', $data, true);
		return $out;
	}
	function show($args) {
		$data['url'] = $this->url;
		if (!is_numeric($args[0])) {
			$can_be = $this->db->select()->from('news')->where('news_curl', $args[0])->get()->row_array();
			if (sizeof($can_be) > 0) {
				$id = $can_be['news_id'];
			}
		} else {
			$id = $args[0];
		}
		$data['page'] = $page = $this->db->select()->from('news')->where('news_id', $id)->get()->row_array();
		if (sizeof($page) <= 0 || $page['news_status'] == 0) redirect('/');
		if ($page['news_pg'] != 0) $data['ph'] = $this->db->select()->from('ph')->where('ph_pg', $page['news_pg'])->order_by('ph_id', 'desc')->get()->result_array();
		$out['data']['crumps'] = " &raquo; <a href='/".$this->url."/".$page['news_id']."'>".$page['news_title']."</a>";
		$out['data']['title'] = $page['news_title'];
		$out['text'] = $this->load->view('modules/news/show', $data, true);
		return $out;
	}
	function news($args) {
		$args[0] = 1;
		return $this->index($args);
	}
	function blogs($args) {
		$args[0] = 2;
		return $this->index($args);
	}
	function actions($args) {
		$args[0] = 3;
		return $this->index($args);
	}
	function discount($args) {
		$args[0] = 4;
		return $this->index($args);
	}
	function actionsdiscount($args) {
		$args[0] = 3;
		$out1 = $this->index($args);
		$actions = $this->db->select()->from('content')->where('page_function','actions')->where('page_model','news')->limit(1)->get()->row_array();
		$out1['text'] = "<h2>Акции</h2>".$out1['text'];
		$out1['text'] .= "<a href='/".$actions['page_curl']."/' title='Все акции'>Все акции</a>";
		$args[0] = 4;
		$out2 = $this->index($args);
		$discount = $this->db->select()->from('content')->where('page_function','discount')->where('page_model','news')->limit(1)->get()->row_array();
		$out2['text'] = "<h2>Скидки</h2>".$out2['text'];
		$out2['text'] .= "<a href='/".$discount['page_curl']."/' title='Все скидки'>Все скидки</a>";
		$out = $out1['text'].$out2['text'];
		return $out;
	}
	function block($args) {
		$data['pages'] = $this->db->select()->from('news')->where('news_status', '1')->where('news_parent', 1)->order_by('news_date', 'desc')->limit(3)->get()->result_array();
		return $this->load->view('modules/news/block', $data, true);
	}
	function get_all_list() {
		return $this->db->select()->from('news')->where('news_status', 1)->order_by('news_date', 'desc')->limit(3)->get()->result_array();		
	}
    function news_bullet() {
		$res = $this->db->select()->from('news')->where('news_status', 1)->where('news_bullet', 1)->where('news_parent', 1)->order_by('news_date', 'desc')->limit(3)->get()->result_array();		
		if(count($res) > 0) {
			return $res;
		} else {
			return $this->db->select()->from('news')->where('news_status', 1)->where('news_parent', 1)->order_by('news_date', 'desc')->limit(3)->get()->result_array();		
		}
	}
    function actions_bullet() {
		$res = $this->db->select()->from('news')->where('news_status', 1)->where('news_bullet', 1)->where('news_parent', 3)->order_by('news_date', 'desc')->limit(3)->get()->result_array();		
		if(count($res) > 0) {
			return $res;
		} else {
			return $this->db->select()->from('news')->where('news_status', 1)->where('news_parent', 3)->limit(3)->order_by('news_date', 'desc')->get()->result_array();
		}
	}
    function discount_bullet() {
		$res = $this->db->select()->from('news')->where('news_status', 1)->where('news_bullet', 1)->where('news_parent', 4)->order_by('news_date', 'desc')->limit(3)->get()->result_array();		
		if(count($res) > 0) {
			return $res;
		} else {
			return $this->db->select()->from('news')->where('news_status', 1)->where('news_parent', 4)->limit(3)->order_by('news_date', 'desc')->get()->result_array();
		}
	}
}